package com.tb.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.tb.domain.User;
import com.tb.downgrade.DowngradeUtil;
import com.tb.feign.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderConterller {

    @Autowired
    private UserService userService;

    @GetMapping("/getById/{id}")
    @SentinelResource(value = "order-server-getById",
                    blockHandler = "orderBlockHandler",
                    blockHandlerClass = DowngradeUtil.class,
                    fallback = "orderFallback",
                    fallbackClass = OrderConterller.class)


    public User getById(@PathVariable("id") Long id) {
//        if (true){
//            throw new RuntimeException("熔断了！！！");
//        }
        return userService.getById(id);
    }


//    public User orderFallback(@PathVariable("id") Long id, Throwable e) {
//        return new User(-1L, "降级了！！！", "服务异常不可调用，稍后访问！！！");
//    }

//    public User orderBlockHandler(@PathVariable("id") Long id, BlockException e) {
//        e.printStackTrace();
//        return new User(-1L, "限流咯", "禁止访问！！！");
//    }

}
